package com.hangzhou.xyj.ui.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.ActionModel;
import com.hangzhou.xyj.model.ActionPageInfoModel;
import com.hangzhou.xyj.model.BannerModel;
import com.hangzhou.xyj.model.DramaCategoryModel;
import com.hangzhou.xyj.model.DramaPageInfoModel;
import com.hangzhou.xyj.model.type.EBannerType;
import com.hangzhou.xyj.net.handler.GetBannerHandler;
import com.hangzhou.xyj.net.handler.GetListHandler;
import com.hangzhou.xyj.net.handler.GetListOfInfoHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetBannerReq;
import com.hangzhou.xyj.net.request.GetListOfInfoReq;
import com.hangzhou.xyj.net.request.GetListReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.FindShowAdapter;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.utils.constants.Constants;

import java.util.List;

/**
 * Created by neo.duan on 2016/1/17.
 */
public class ShowActivity extends BaseActivity implements FindShowAdapter.OnDramaChangeListener {
    private RecyclerView mRecyclerView;
    private FindShowAdapter mAdapter;
    private int mDramaId;

    @Override
    public void initTop() {
        enableTitle(true, "演出");
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_show);
    }

    @Override
    public void initViews() {
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_my_group);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new FindShowAdapter(mRecyclerView, null));
    }

    @Override
    public void initDatas() {
        //请求Banner数据
        GetBannerReq getBannerReq = new GetBannerReq(EBannerType.SHOW);
        HttpLoader.getInstance().sendRequest(new GetBannerHandler(getBannerReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                //更新banner数据
                List<BannerModel> bannerList = (List<BannerModel>) model;
                mAdapter.updateBannerData(bannerList);
            }
        }, 0));
        getDramaList();
    }

    @Override
    public void initListeners() {
        mAdapter.setOnDramaChangeListener(this);
        mAdapter.setOnLoadingListener(new BaseLoadingAdapter.OnLoadingListener() {
            @Override
            public void loading() {
                mRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getShowList(mDramaId, mAdapter.getCurrentPage(), false);
                    }
                }, 800);
            }
        });
    }


    /**
     * 获取剧种类别
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/12 16:13
     */
    public void getDramaList() {
        GetListReq getListReq = new GetListReq();
        HttpLoader.getInstance().sendRequest(new GetListHandler(getListReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                List<DramaCategoryModel> list = ((DramaPageInfoModel) model).getDramaCategoryList();
                list.get(0).setIsSelected(true);//设置第一个为选中
                mAdapter.updateDramaCategoryData(list);
                //获取演出列表
                getShowList(list.get(0).getId(), 1, true);
            }
        }, 0));
    }


    /**
     *
     * 获取演出列表
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/16 10:55
     */
    public void getShowList(int catId, final int currentPage, final boolean isReload) {
        if (isReload) {
            mDramaId = catId;
            mAdapter.setIsReload(true);
        } else {
            mAdapter.setIsReload(false);
        }
        //请求数据
        GetListOfInfoReq getListOfInfoReq = new GetListOfInfoReq(GetListOfInfoReq.TYPE_SHOW, catId, currentPage, Constants.PAGE_SIZE);
        HttpLoader.getInstance().sendRequest(new GetListOfInfoHandler(getListOfInfoReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                List<ActionModel> list = ((ActionPageInfoModel) model).getActionList();
                if (currentPage == 1) {
                    mAdapter.load(list);
                } else {
                    mAdapter.update(list);
                }
            }

            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

            @Override
            public void onError() {
                super.onError();
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

        }, 0));
    }

    @Override
    public void OnDramaChange(int catId, boolean isReload) {
        getShowList(catId, 1, true);
    }
}
